﻿#include "TestScene.h"
#include "Engine.h"
#include "SkinnedMeshRenderer.h"
#include "Animator.h"
#include "Graphics.h"
#include "Resources.h"
#include <stb/stb_image.h>
#include <vector>

using namespace DYGraphics;
using namespace DYEngine;

TestScene::TestScene() : Scene("TestScene"), mGo(nullptr) 
{
	mCamController.BindCamera(mCamera);
}

TestScene::~TestScene()
{
	
}

void TestScene::Load()
{
	// 加载FBX
	const char* fbxPath = "../asset/chr100_000_00.FBX";
	FbxLoader fbxLoader;
	fbxLoader.Import(fbxPath);
	SkinnedMeshPtr skinnedMesh = fbxLoader.LoadSkinnedMesh();
	skinnedMesh->Apply();

	std::vector<AnimationDataPtr> animDatas = fbxLoader.LoadAnimation();

	// 加载贴图
	mTexture = Image::Load("../asset/chr100_000_00.png");

	// 设置材质
	Shader* shader = new Shader("../asset/shader/texture.vs", "../asset/shader/texture.fs");
	Material* material = new Material(shader);
	material->SetFloat3("lightColor", glm::vec3(0.5f, 0.5f, 0.5f));
	material->SetFloat3("lightDir", glm::vec3(1.0f, 1.0f, 1.0f));
	material->SetTexture("texture0", mTexture.get());

	// 创建Renderer
	SkinnedMeshRenderer* renderer = new SkinnedMeshRenderer();
	renderer->SetMesh(skinnedMesh);
	renderer->SetMaterial(material);
	renderer->Init();

	// 创建GameObject
	mGo = new GameObject();
	mGo->AddComponent(renderer);
	AddNode(mGo);

	Transform* goT = mGo->GetTransform();
	// goT->SetScale(glm::vec3(100, 100, 100));
	// goT->SetRotation(glm::vec3(90, 0, 0));

	// 创建动画
	mAnimator = new Animator();
	mAnimator->Bind(skinnedMesh);
	mAnimator->SetAnimData(animDatas[1]);
	mGo->AddComponent(mAnimator);

	// 设置相机
	mCamera->SetProjCamera(45, (float)Camera::sWidth / Camera::sHeight, 0.01f, 100.0f);
	Transform* camT = mCamera->GetTransform();
	camT->SetPosition(glm::vec3(0, 0, 2));
}

void TestScene::Update()
{
	mAnimator->Update();
}